package com.cbgolf.oa.widget.pickview.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import com.cbgolf.oa.R;
import com.cbgolf.oa.util.ViewUtils;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;


/**
 * Created by Sai on 15/11/22.
 */
public class TimePickerView extends BasePickerView implements View.OnClickListener {

  public enum Type {
    ALL, YEAR_MONTH_DAY, HOURS_MINS, MONTH_DAY_HOUR_MIN, YEAR_MONTH
  }// 四种选择模式，年月日时分，年月日，时分，月日时分

  WheelTime wheelTime;
  private View btnSubmit, btnSubmit1, btnCancel;
  private View lableVi;
  private TextView tvTitle;
  private TextView tvLableYear, tvLableMonth, tvLableDay, tvLableHour, tvLableMinute;
  private static final String TAG_SUBMIT = "submit";
  private static final String TAG_CANCEL = "cancel";
  private OnTimeSelectListener timeSelectListener;
  private View pickerVi;
  private Context context;

  private void initWheel(Type type) {
    // -----确定和取消按钮
    btnSubmit = findViewById(R.id.btnSubmit);
    btnSubmit.setTag(TAG_SUBMIT);
    btnCancel = findViewById(R.id.btnCancel);
    btnCancel.setTag(TAG_CANCEL);
    btnSubmit.setOnClickListener(this);
    btnCancel.setOnClickListener(this);
    // 顶部标题
    tvTitle = (TextView) findViewById(R.id.tvTitle);
    lableVi = findViewById(R.id.year_m_day_ll);
    tvLableYear = (TextView) findViewById(R.id.tv_pickview_lable_year);
    tvLableMonth = (TextView) findViewById(R.id.tv_pickview_lable_month);
    tvLableDay = (TextView) findViewById(R.id.tv_pickview_lable_day);
    tvLableHour = (TextView) findViewById(R.id.tv_pickview_lable_hour);
    tvLableMinute = (TextView) findViewById(R.id.tv_pickview_lable_minute);
    initLables(type);
    // ----时间转轮
    pickerVi = findViewById(R.id.timepicker);
    wheelTime = new WheelTime(pickerVi, type);

    // 默认选中当前时间
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH);
    int day = calendar.get(Calendar.DAY_OF_MONTH);
    int hours = calendar.get(Calendar.HOUR_OF_DAY);
    int minute = calendar.get(Calendar.MINUTE);
    wheelTime.setAddLable(isAddLable);
    wheelTime.setPicker(year, month, day, hours, minute);
  }

  private void initLables(Type type) {
    ViewUtils.setVisible(tvLableYear, true);
    ViewUtils.setVisible(tvLableMonth, true);
    ViewUtils.setVisible(tvLableDay, true);
    ViewUtils.setVisible(tvLableHour, true);
    ViewUtils.setVisible(tvLableMinute, true);
    switch (type) {
      case YEAR_MONTH_DAY:
        ViewUtils.setVisible(tvLableHour, false);
        ViewUtils.setVisible(tvLableMinute, false);
        break;
      case HOURS_MINS:
        ViewUtils.setVisible(tvLableYear, false);
        ViewUtils.setVisible(tvLableMonth, false);
        ViewUtils.setVisible(tvLableDay, false);
        break;
      case MONTH_DAY_HOUR_MIN:
        ViewUtils.setVisible(tvLableYear, false);
        break;
      case YEAR_MONTH:
        ViewUtils.setVisible(tvLableDay, false);
        ViewUtils.setVisible(tvLableHour, false);
        ViewUtils.setVisible(tvLableMinute, false);
        break;
      default:
        break;
    }
  }

  public void setLableVisible(boolean visible) {
    ViewUtils.setVisible(lableVi, visible);
  }

  public void setPickerBackgroudColor(int color) {
    ViewUtils.setBackgroundColor(context, pickerVi, color);
  }

  public TimePickerView(Context context, Type type) {
    super(context);
    this.context = context;
    LayoutInflater.from(context).inflate(R.layout.pickerview_time, contentContainer);
    initWheel(type);

  }

  private boolean isAddLable;

  public TimePickerView(Context context, Type type, boolean addLable) {
    super(context);
    this.context = context;
    this.isAddLable = addLable;
    LayoutInflater.from(context).inflate(R.layout.pickerview_time, contentContainer);
    initWheel(type);

  }

  /**
   * 设置可以选择的时间范围
   */
  public void setRange(int startYear, int endYear) {
    wheelTime.setStartYear(startYear);
    wheelTime.setEndYear(endYear);
  }

  /**
   * 设置选中时间
   */
  public void setTime(Date date) {
    Calendar calendar = Calendar.getInstance();
    if (date == null) {
      calendar.setTimeInMillis(System.currentTimeMillis());
    } else {
      calendar.setTime(date);
    }
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH);
    int day = calendar.get(Calendar.DAY_OF_MONTH);
    int hours = calendar.get(Calendar.HOUR_OF_DAY);
    int minute = calendar.get(Calendar.MINUTE);
    wheelTime.setPicker(year, month, day, hours, minute);
  }

  // /**
  // * 指定选中的时间，显示选择器
  // *
  // * @param date
  // */
  // public void show(Date date) {
  // Calendar calendar = Calendar.getInstance();
  // if (date == null)
  // calendar.setTimeInMillis(System.currentTimeMillis());
  // else
  // calendar.setTime(date);
  // int year = calendar.get(Calendar.YEAR);
  // int month = calendar.get(Calendar.MONTH);
  // int day = calendar.get(Calendar.DAY_OF_MONTH);
  // int hours = calendar.get(Calendar.HOUR_OF_DAY);
  // int minute = calendar.get(Calendar.MINUTE);
  // wheelTime.setPicker(year, month, day, hours, minute);
  // show();
  // }

  /**
   * 设置是否循环滚动
   */
  public void setCyclic(boolean cyclic) {
    wheelTime.setCyclic(cyclic);
  }

  @Override
  public void onClick(View v) {
    String tag = (String) v.getTag();
    if (tag.equals(TAG_CANCEL)) {
      dismiss();
      return;
    } else {
      if (timeSelectListener != null) {
        try {
          Date date = WheelTime.dateFormat.parse(wheelTime.getTime());
          timeSelectListener.onTimeSelect(date);
        } catch (ParseException e) {
          e.printStackTrace();
        }
      }
      dismiss();
      return;
    }
  }

  public interface OnTimeSelectListener {

    public void onTimeSelect(Date date);
  }

  public void setOnTimeSelectListener(OnTimeSelectListener timeSelectListener) {
    this.timeSelectListener = timeSelectListener;
  }

  public void setTitle(String title) {
    tvTitle.setText(title);
  }
}
